package net.caixiaomi.info.ui.discovery;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;

import net.caixiaomi.info.R;
import net.caixiaomi.info.adapter.OpenPrizeDetailAdapter;
import net.caixiaomi.info.adapter.OpenPrizeNumAdapter;
import net.caixiaomi.info.base.BaseActivity;
import net.caixiaomi.info.base.progressbar.CircularProgressBar;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.model.BaseCallModel;
import net.caixiaomi.info.model.DltPrizeEntity;
import net.caixiaomi.info.model.OpenPrizeDetailEntity;
import net.caixiaomi.info.model.OpenPrizeDetailNumEntity;
import net.caixiaomi.info.model.SzcPrizePageInfo;
import net.caixiaomi.info.net.RequestService;
import net.caixiaomi.info.net.ResponseCallback;
import net.caixiaomi.info.net.ResponseError;
import net.caixiaomi.info.net.RetrofitManage;
import net.caixiaomi.info.util.Constants;
import net.caixiaomi.smartrefresh.layout.SmartRefreshLayout;
import net.caixiaomi.smartrefresh.layout.api.RefreshLayout;
import net.caixiaomi.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;
import retrofit2.Call;

import static android.R.id.list;

/**
 * Created by mac on 2018/9/7.
 */

public class OpenPrizeDetailActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener, OnRefreshListener {

    @BindView(R.id.title)
    TextView mTitle;

    @BindView(list)
    RecyclerView mListView;
    @BindView(R.id.refresh)
    SmartRefreshLayout mRefresh;
    @BindView(R.id.progress)
    CircularProgressBar mProgress;

    private OpenPrizeDetailAdapter mAdapter;
    private Context mContext;

    private String mLotteryClassify;
    private int termNum;

    private View headerView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        findViewById(R.id.frag_group).setFitsSystemWindows(true);
        mContext=this;
        mAdapter=new OpenPrizeDetailAdapter(R.layout.prize_detail_item_layout);
        mRefresh.setOnRefreshListener(this);
        mListView.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter.setOnItemClickListener(this);
        mListView.setAdapter(mAdapter);
        mLotteryClassify=getIntent().getStringExtra("lotteryClassify");
        termNum=getIntent().getIntExtra("termNum",0);

        headerView = LayoutInflater.from(mContext).inflate(R.layout.open_prize_detail_header_layout, null);
        mAdapter.addHeaderView(headerView);
        View footer=LayoutInflater.from(mContext).inflate(R.layout.prize_footer_layout, null);
        mAdapter.addFooterView(footer);

        loadData();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        loadData();
    }

    @Override
    protected int layout() {
        return R.layout.frag_home;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }

    private void loadData(){
        JSONObject params = new JSONObject();
        params.put("lotteryClassify", mLotteryClassify);
        params.put("termNum", termNum);
        RequestService requestService = RetrofitManage.getInstance().getRequestService();
        Call<BaseCallModel<OpenPrizeDetailEntity>> dis = requestService.getDiscoverySZCDetail(params);

        dis.enqueue(new ResponseCallback<BaseCallModel<OpenPrizeDetailEntity>>() {
            @Override
            public void onSuccess(BaseCallModel<OpenPrizeDetailEntity> data) {
                mProgress.setVisibility(View.GONE);
                mRefresh.finishRefresh();
                List<OpenPrizeDetailNumEntity> list = data.data.getSuperLottoRewardDetailsList();
                mAdapter.setNewData(list);
                mAdapter.notifyDataSetChanged();
                initHeaderView(data.data);
            }

            @Override
            public void onFail(ResponseError error) {
                mProgress.setVisibility(View.GONE);
                mRefresh.finishRefresh();
            }

            @Override
            public void onError() {
                mProgress.setVisibility(View.GONE);
                mRefresh.finishRefresh();
            }
        });
    }

    private void initHeaderView(OpenPrizeDetailEntity entity){
        TextView sellTv=headerView.findViewById(R.id.sell_money_cur);
        TextView prizeTv=headerView.findViewById(R.id.prize_money_cur);
        TextView prizeName=headerView.findViewById(R.id.prize_name);
        TextView prizePeriod=headerView.findViewById(R.id.prize_period);
        TextView prizeDate=headerView.findViewById(R.id.prize_date);
        sellTv.setText(entity.getSellAmount());
        prizeTv.setText(entity.getPrizes());
        prizeName.setText(entity.getLotteryName());
        prizePeriod.setText(entity.getPeriod());
        prizeDate.setText(entity.getPrizeDate());
        LinearLayout ballGroup=headerView.findViewById(R.id.ball_group);
        for(int i=0;i<5;i++){
            LinearLayout ll= (LinearLayout) ballGroup.getChildAt(i);
            TextView tv= (TextView) ll.getChildAt(0);
            tv.setText(entity.getRedPrizeNumList().get(i));
            tv.setBackgroundResource(R.drawable.red_ball);
        }
        for(int i=5;i<7;i++){
            LinearLayout ll= (LinearLayout) ballGroup.getChildAt(i);
            TextView tv= (TextView) ll.getChildAt(0);
            tv.setText(entity.getBluePrizeNumList().get(i-5));
            tv.setBackgroundResource(R.drawable.blue_ball);
        }
    }
}
